﻿// -----------------------------------------------------------------------
// <copyright file="Dog.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Cspl.AnimalFight.Lib.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Cspl.AnimalFight.Lib.Interfaces;
using Cspl.AnimalFight.Lib.EventHandlers;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Dog : AnimalBase, IAttack, IDefend, IAttackable
    {
        #region Fields

        private DogMood _mood;
        public event AttackDelegate AttackEvent;
        public event DefendDelegate DefendEvent;

        #endregion

        #region Properties

        public bool IsDefending { get; set; }

        public DogMood Mood
        {
            get { return _mood; }
            set { _mood = value; }
        }

        #endregion

        #region Methods

        public void Attack(string targetName)
        {
            int theRealAttackPower = 0;

            if (this.Mood == DogMood.Bad)
            {
                theRealAttackPower = (int)(0.5 * this.AP);
            }
            else
            {
                theRealAttackPower = this.AP;
            }

            if (AttackEvent != null)
            {
                AttackEventHandler eventHandler = new AttackEventHandler
                {
                    AnimalTargetName = targetName,
                    AP = theRealAttackPower
                };

                AttackEvent(this, eventHandler);
            }
        }

        public void Defend()
        {
            if (DefendEvent != null)
            {
                DefendEvent(this, new DefendEventHandler
                {
                    AnimalName = this.Name,
                    Target = Animals.Dog
                });
            }
        }

        public void Attacked(int ap)
        {
            if (this._hasEscaped)
            {
                Console.WriteLine("  {0} has escaped. Cannot be attacked.", this.Name);
            }
            else if (this._isDead)
            {
                Console.WriteLine("  {0} is already dead. Cannot be attacked.", this.Name);
            }
            else
            {
               
                int realDp = this.DP;

                //check whether the dog is defending or not, and determine the value of realDp 
                if (this.IsDefending)
                {
                    realDp += 10;
                    Console.WriteLine("  {0} is defend. Current DP is increased 10", this.Name);
                }
                // check whether the dog is a bad dog or not, and determine the value of realDp 
                if (this.Mood == DogMood.Bad)
                {
                    realDp -= 10;
                    Console.WriteLine("  {0} is a Bad Dog. Current DP is decreased 10", this.Name);
                }
                else
                {
                    Console.WriteLine("  {0} is a Good Dog.", this.Name);
                }

                if (realDp < ap)
                {
                    Console.WriteLine("  {0} is being attacked. Current HP={1}", this.Name, this.HP);
                    // if the attack is less powerfull than defense then it will be absorbed
                    int HPDecreaser = ap - realDp;

                    // Prevent negative value, Negative value will increase HP instead. So negative value is set to 0.
                    this.HP -= HPDecreaser < 0 ? 0 : HPDecreaser;

                    Console.WriteLine("  HP is decreased {0}", HPDecreaser);

                    if (this.HP > 0)
                    {
                        //only displayed when HP > 0. 
                        //Because HP<= 0 means that animal is dead
                        Console.WriteLine("  Current HP for {0}={1}", this.Name, this.HP);
                    }
                    else
                    {
                        Console.WriteLine("  The {0} is dead.", this.Name);
                        this.IsDead = true;
                    }
                }
                else
                {
                    Console.WriteLine("  {0} cannot be attacked because it's defend power is EQUAL or BIGGER than attacker attack power.", this.Name);
                }
            }
        }

        #endregion


        //public Dog(DogMood Mood) {this.Mood = Mood }

              
    }

    public enum DogMood { Good, Bad }
}
